<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/dtree.css}" >
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/font/dtreefont.css}" >
</head>
<body>

<span th:replace="common_page::common"></span>

<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" id="orderSearchForm" action="">
                    <input type="hidden" id="tradeStatus" name="tradeStatus" value="-1">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">主订单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="mainOrderNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">订单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="orderNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">下单时间</label>
                            <div class="layui-input-inline">
                                <input type="text" id="startCreateDate" name="startCreateDate" class="layui-input"  readonly >
                            </div>
                        </div>
                        至&nbsp;&nbsp;
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" id="endCreateDate" name="endCreateDate" class="layui-input"  readonly >
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userId" autocomplete="off" class="layui-input">
                            </div>
                        </div>



                        <div class="layui-inline">
                            <label class="layui-form-label">店铺ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="shopId" autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">支付状态</label>
                            <div class="layui-input-inline">
                                <select id="payStatus" name="payStatus"  lay-search="">
                                    <option value="-1">全部</option>
                                    <option value="0">未支付</option>
                                    <option value="1">已支付</option>
                                    <option value="3">线下支付已到账</option>
                                    <option value="4">取消支付</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="reset" class="layui-btn layui-btn-primary" id="data-reset-btn" > 重 置</button>
                        </div>
                    </div>
                </form>

            </div>
        </fieldset>

        <div class="layui-tab" lay-filter="orderTablFilter">
            <ul class="layui-tab-title">
                <li class="layui-this" trade-status="-1" lay-id="tab_index_0_">全部</li>
                <li lay-id="tab_index_1_" trade-status="0">待付款</li>
                <li lay-id="tab_index_2_" trade-status="1">待收货</li>
                <li lay-id="tab_index_3_" trade-status="2">已取消</li>
                <li lay-id="tab_index_4_" trade-status="3">已完成</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <script type="text/html" id="tab_index_0_toolbarDemo">
                        <div class="layui-btn-container">
                            <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>

                        </div>
                    </script>

                    <table class="layui-hide" id="tab_index_0_orderTable" lay-filter="tab_index_0_orderTableFilter"></table>

                    <script type="text/html" id="tab_index_0_orderTableBar">
                        <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
                    </script>

                </div>
                <div class="layui-tab-item">
                    <script type="text/html" id="tab_index_1_toolbarDemo">
                        <div class="layui-btn-container">
                            <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>

                        </div>
                    </script>

                    <table class="layui-hide" id="tab_index_1_orderTable" lay-filter="tab_index_1_orderTableFilter"></table>

                    <script type="text/html" id="tab_index_1_orderTableBar">
                        <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
                    </script>
                </div>
                <div class="layui-tab-item">

                    <script type="text/html" id="tab_index_2_toolbarDemo">
                        <div class="layui-btn-container">
                            <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>

                        </div>
                    </script>

                    <table class="layui-hide" id="tab_index_2_orderTable" lay-filter="tab_index_2_orderTableFilter"></table>

                    <script type="text/html" id="tab_index_2_orderTableBar">
                        <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
                    </script>
                </div>
                <div class="layui-tab-item">

                    <script type="text/html" id="tab_index_3_toolbarDemo">
                        <div class="layui-btn-container">
                            <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>

                        </div>
                    </script>

                    <table class="layui-hide" id="tab_index_3_orderTable" lay-filter="tab_index_3_orderTableFilter"></table>

                    <script type="text/html" id="tab_index_3_orderTableBar">
                        <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
                    </script>
                </div>
                <div class="layui-tab-item">


                    <script type="text/html" id="tab_index_4_toolbarDemo">
                        <div class="layui-btn-container">
                            <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>

                        </div>
                    </script>

                    <table class="layui-hide" id="tab_index_4_orderTable" lay-filter="tab_index_4_orderTableFilter"></table>

                    <script type="text/html" id="tab_index_4_orderTableBar">
                        <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
                    </script>
                </div>
            </div>
        </div>


    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script>
    layui.config({
        base: basePath+'/lib/layui_ext/dtree/' //配置 layui 第三方扩展组件存放的基础目录
    }).extend({
        dtree: 'dtree' //定义该组件模块名
    }).use(['form', 'table','tree','dtree','laydate','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var element = layui.element;
        var tableCache = new Map();
        var currentTabIndex="tab_index_0_";

        element.on('tab(orderTablFilter)',function(){
            var layId = this.getAttribute("lay-id");
            $("#tradeStatus").val(this.getAttribute("trade-status"));

            initOrderTable(layId);
        });

        $.fn.serializeJson=function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        };



        var laydate = layui.laydate;

        //初始化时间控件
        laydate.render({
            elem: '#startCreateDate' //指定元素
            ,type: 'datetime'
        });

        //初始化时间控件
        laydate.render({
            elem: '#endCreateDate' //指定元素
            ,type: 'datetime'
        });




        function getFormParam()
        {
            var formParam = $("#orderSearchForm").serializeJson();
            return formParam;
        }

        function initOrderTable(tableIndex)
        {
            currentTabIndex = tableIndex;
            if(tableCache.get(currentTabIndex)!=null){
                table.reload(currentTabIndex+'orderTable', {
                    page: {
                        curr: 1
                    }
                    , where: getFormParam()
                }, 'data');
                return ;
            }

            var tableHandler = table.render({
                elem: '#'+currentTabIndex+'orderTable',
                url: "[[@{/}]]order/list",
                method:'POST',
                where:getFormParam(),
                toolbar: '#'+currentTabIndex+'toolbarDemo',
                defaultToolbar: ['filter',  {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                done:function(res, curr, count)
                {
                },
                cols: [
                    [
                        {type: "checkbox", width: 50},
                        {field: 'id', width: 200,  title: '主键'},
                        {field: 'mainOrderNo', width: 300,  title: '主订单编号'},
                        {field: 'orderNo', width: 300,  title: '订单编号',event:'openDetailDialogDevent',
                            templet: function (row) {
                                return "<a href='#'   style='color: blue;text-decoration:underline;'>"+row.orderNo+"</a>";
                            }},
                        {field: 'userId', width: 200,  title: '用户ID'},
                        {field: 'shopId', width: 200,  title: '店铺ID'},
                        {field: 'payMethod', width: 100, title: '支付方式',
                            templet: function (row) {
                                if(row.payMethod == "1"){
                                    return "线上支付"
                                }else if(row.payMethod == "2"){
                                    return "线下支付"
                                }
                                return row.payMethod;
                            }},
                        {field: 'payType', width: 100, title: '交易类型',
                            templet: function (row) {
                                if(row.payType == -1){
                                    return "未确定"
                                }else if(row.payType == 0){
                                    return "微信"
                                }else if(row.payType == 1){
                                    return "支付宝"
                                }
                                return row.payType;
                            }},
                        {field: 'payStatus', width: 100, title: '支付状态',
                            templet: function (row) {
                                if(row.payStatus == "0"){
                                    return "未支付";
                                }else if(row.payStatus == "1"){
                                    return "<a   style='color: green;'>已支付</a>";
                                }else if(row.payStatus == "3"){
                                    return "<a   style='color: green;'>线下支付已到账</a>";
                                }else if(row.payStatus == "4"){
                                    return "<a   style='color: red;'>取消支付</a>";
                                }
                                return row.payStatus;
                            }},
                        {field: 'tradeStatus', width: 100, title: '交易状态',
                            templet: function (row) {
                                if(row.tradeStatus == "0"){
                                    return "待付款";
                                }else if(row.tradeStatus == "1"){
                                    return "待收货";
                                }else if(row.tradeStatus == "2"){
                                    return "<a   style='color: red;'>已取消</a>";
                                }else if(row.tradeStatus == "3"){
                                    return "<a   style='color: green;'>已完成</a>";
                                }
                                return row.tradeStatus;
                            }},
                        {field: 'freightAmount', width: 100,  title: '运费金额'},
                        {field: 'redPackageAmount', width: 100,  title: '红包金额'},
                        {field: 'couponAmount', width: 100,  title: '优惠券金额'},
                        {field: 'orderAmount', width: 100,  title: '订单金额'},
                        {field: 'outerTradeNo', width: 100,  title: '交易流水号'},
                        {field: 'srcType', width: 100, title: '购买渠道',
                            templet: function (row) {
                                if(row.srcType == 1){
                                    return "PC"
                                }else if(row.srcType == 2){
                                    return "APP"
                                }
                                return row.srcType;
                            }},
                        {field: 'payDate', width: 150,  title: '支付时间'},
                        {field: 'paymentDeadlineTime', width: 150,  title: '支付截止时间'},
                        {field: 'createDate', width: 230,  title: '创建时间'},
                        {field: 'cancelDate', width: 230,  title: '取消订单时间'},
                        {field: 'cancelRemark', width: 230,  title: '取消订单备注'},
                        {title: '操作', minWidth: 300, toolbar: '#'+tableIndex+'orderTableBar', align: "center"}
                    ]
                ],
                limits: [15,30,100,200],
                limit: 15,
                page: true,
                skin: 'line'
            });

            tableCache.set(currentTabIndex,tableHandler);

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //执行搜索重载
                table.reload(currentTabIndex+'orderTable', {
                    page: {
                        curr: 1
                    }
                    , where: data.field
                }, 'data');

                return false;
            });




            /**
             * toolbar监听事件
             */
            table.on('toolbar('+currentTabIndex+'orderTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加订单',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        area: ['100%', '100%'],
                        content: "[[@{/order/addPage}]]"
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'deleteToolbar') {  // 监听删除操作
                    var checkStatus = table.checkStatus(currentTabIndex+'orderTable')
                        , data = checkStatus.data;
                    if(data==null||data.length<=0)
                    {
                        layer.msg("请选择要操作的记录");
                        return;
                    }
                    layer.confirm('确定删除订单?', function (index) {
                        $.ajax({
                            url:"[[@{/}]]order/delete/ids",
                            contentType: "application/json; charset=utf-8",
                            type:'DELETE',
                            data:JSON.stringify(data),
                            success:function(data){
                                layer.msg(data.msg);

                                //执行搜索重载
                                table.reload(currentTabIndex+'orderTable', {
                                    page: {
                                        curr: 1
                                    }
                                    , where: data.field
                                }, 'data');

                            },
                            complete:function(data)
                            {
                                layer.close(index);
                            }
                        });
                    });
                }
            });





            //监听表格复选框选择
            table.on('checkbox('+currentTabIndex+'orderTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool('+currentTabIndex+'orderTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event == 'detail') {
                    openDetailDialog(data.id);
                }else if(obj.event=="openDetailDialogDevent")
                {
                    openDetailDialog(data.id);
                }else if(obj.event=="cancelOrder")
                {
                    var index = layer.open({
                        title: '取消订单',
                        type: 2,
                        shade: 0.2,
                        maxmin:false,
                        area: ['50%', '50%'],
                        content: "[[@{/}]]order/cancel/page/"+data.orderNo
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                }else if (obj.event == 'update') {
                    openUpdateDialog(data.id);
                }
            });
        }


        function openDetailDialog(id)
        {
            var index = layer.open({
                title: '订单详情',
                type: 2,
                shade: 0.2,
                maxmin:true,

                area: ['100%', '100%'],
                content: '[[@{/}]]order/detailPage/'+id
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;

        }


        function openUpdateDialog(id)
        {
            var index = layer.open({
                title: '修改订单',
                type: 2,
                shade: 0.2,
                maxmin:true,

                area: ['100%', '100%'],
                content: '[[@{/}]]order/editPage/'+id
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;

        }
        initOrderTable("tab_index_0_");


    });
</script>

</body>
</html>